package init;

import configuration.*;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by kzdatd on 2020/1/13.
 */
public class InitConf {

    private static Logger logger = LoggerFactory.getLogger(InitConf.class);

    public static String TOPIC_OUT;
    public static String STREAMID;
    public static String STREAMGOURPID;
    public static String DBURL;
    public static String DBUSER;
    public static String DBPWD;
    public static String TOPIC_IN;
    public static String KAFKA_BOOTSTRAP;


    public static boolean debug=false;
    //一次从服务器读取记录条数
    public static int FETCH_SIZE = 100;
    public static KMXServerConfig kmxServerConfig = null;
    public static KafkaServerConfig kafkaServerConfig = null;
    public static SQLServerConfig sqlServerConfig = null;
    public static ModelerServerConfig modelerServerConfig = null;
    public static DemoConfig demoConfig = null;


    public static Map<String,String> matchMap = null;
    public void init(){
        Properties properties = new Properties();
        String path = "D:\\workSpace\\socket\\datastream\\target\\classes\\env_local.properties";
        File file = new File(path);
        try {
            properties.load(new InputStreamReader(new FileInputStream(file),"utf-8"));
            TOPIC_IN = properties.get("TOPIC_IN").toString();
            TOPIC_OUT = properties.get("TOPIC_OUT").toString();
            STREAMID = properties.get("STREAMID").toString();
            STREAMGOURPID = properties.get("STREAMGOURPID").toString();
            DBURL = properties.get("DBURL").toString();
            DBUSER = properties.get("DBUSER").toString();
            DBPWD = properties.get("DBPWD").toString();
            KAFKA_BOOTSTRAP = properties.get("KAFKA_BOOTSTRAP").toString();
            config();
            matchMap();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void  config(){
        try {
            logger.info("prepare:loading configure files...");

            String sqlserverPath=System.getProperty("user.dir") + "/common/configs/sqlserver.yaml";
            sqlServerConfig = SQLServerConfig.initFromFile(sqlserverPath);
            logger.info(sqlServerConfig.toString());

            String kmxPath=System.getProperty("user.dir") + "/common/configs/kmx.yaml";
            kmxServerConfig = KMXServerConfig.initFromFile(kmxPath);
            logger.info(kmxServerConfig.toString());

            String kafkaPath=System.getProperty("user.dir") + "/common/configs/kafka.yaml";
            kafkaServerConfig = KafkaServerConfig.initFromFile(kafkaPath);
            logger.info(kafkaServerConfig.toString());

            String modelerPath=System.getProperty("user.dir") + "/common/configs/modeler.yaml";
            modelerServerConfig = ModelerServerConfig.initFromFile(modelerPath);
            logger.info(modelerServerConfig.toString());


            String demoPath=System.getProperty("user.dir") + "/common/configs/demo.yaml";
            demoConfig = DemoConfig.initFromFile(demoPath);
            logger.info(demoConfig.toString());

            logger.info("prepare:done.");
        } catch (FileNotFoundException e) {
            logger.error("occur exception:"+e);
            e.printStackTrace();
            System.exit(0);
        }
    }


    public static void matchMap(){
        matchMap = new HashMap<>();
        matchMap.put("加载吨位","load_tonnage");
        matchMap.put("F12","F12");
        matchMap.put("P17_YL","P17_YL");
        matchMap.put("电机转速","AC5000PZD_in3");
    }
}
